﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Text;
using Entities;

namespace Distribution.Data
{
    public class DSetting
    {
        public static ESettings GetAll ()
        {
            SqlCeConnection _Con = null;
            StringBuilder query = new StringBuilder();

            try
            {
                _Con = Helper.GetConnection();

                query.Append("SELECT SettingId, SettingKey, SettingName, SettingValue ");
                query.Append("FROM tb_Settings ");
                
                SqlCeCommand command = new SqlCeCommand(query.ToString(), _Con);
                command.CommandType = CommandType.Text;

                SqlCeDataReader dr = command.ExecuteReader();
                ESettings eSettings = new ESettings();

                while (dr.Read())
                {
                    SettingsType settingType = (SettingsType)Int32.Parse(dr.GetString(dr.GetOrdinal("SettingKey")));

                    switch (settingType)
                    {
                        case SettingsType.VersionDatabase:
                            eSettings.VersionDatabase = BuildSetting(dr, settingType);
                            break;
                        case SettingsType.DistributionAppMode:
                            ESetting distributionSetting = BuildSetting(dr, settingType);
                            eSettings.DistributionApplicationMode = distributionSetting.Value.Equals(0) ? DistributionApplicationMode.GPS : DistributionApplicationMode.Inspection;
                            break;
                        case SettingsType.Handheld:
                            eSettings.Handheld = BuildSetting(dr, settingType);
                            break;
                        case SettingsType.Project:
                            eSettings.Project = BuildSetting(dr, settingType);
                            break;
                        case SettingsType.Timezone:
                            eSettings.Timezone = BuildSetting(dr, settingType);
                            break;
                        case SettingsType.User:
                            eSettings.User = BuildSetting(dr, settingType);
                            break;
                        case SettingsType.Utility:
                            eSettings.Utility = BuildSetting(dr, settingType);
                            break;
                        case SettingsType.Vendor:
                            eSettings.Vendor = BuildSetting(dr, settingType);
                            break;
                        case SettingsType.Inspector:
                            eSettings.Inspector = BuildSetting(dr, settingType);
                            break;
                        case SettingsType.MaxMeterPerPole:
                            ESetting maxMeterPerPole = BuildSetting(dr, settingType);

                            eSettings.MaxMeterPerPole = maxMeterPerPole.Value;
                            break;
                        case SettingsType.ReceptionMode:
                            eSettings.ReceptionMode = BuildSetting(dr, settingType);
                            break;
                    }
                }

                return eSettings;
            }
            finally
            {
                if (_Con != null)
                {
                    _Con.Close();
                }
            }                 
        }

        public static void Save(ESettings settings)
        {
            SqlCeTransaction tx = null;
            SqlCeConnection con = null;

            try
            {
                con = Helper.GetConnection();
                tx = con.BeginTransaction();

                Save(settings.Timezone, con, tx);
                Save(settings.Utility, con, tx);
                Save(settings.User, con, tx);
                Save(settings.Vendor, con, tx);
                Save(settings.Project, con, tx);
                Save(settings.Inspector, con, tx);
                Save(settings.ReceptionMode, con, tx);

                tx.Commit();
            }
            catch
            {
                if (tx != null)
                    tx.Rollback();

                throw;
            }
            finally
            {
                if (con != null)
                    con.Close();
            }
        }

        private static void Save(ESetting settings, SqlCeConnection con, SqlCeTransaction tx)
        {
            StringBuilder query = new StringBuilder();

            query.Append("UPDATE tb_Settings ");
            query.Append("SET SettingName = @SettingName, ");
            query.Append("SettingValue = @SettingValue ");
            query.Append("WHERE SettingId = @SettingId ");

            SqlCeCommand command = new SqlCeCommand(query.ToString(), con, tx);
            command.CommandType = CommandType.Text;

            if (!String.IsNullOrEmpty(settings.Name))
                command.Parameters.Add(Helper.CreateNVarCharParameter("@SettingName", 100, ParameterDirection.Input, false, settings.Name));
            else
                command.Parameters.Add(Helper.CreateNVarCharParameter("@SettingName", 100, ParameterDirection.Input, false, DBNull.Value));

            if (settings.Value != Int32.MinValue)
                command.Parameters.Add(Helper.CreateIntParameter("@SettingValue", ParameterDirection.Input, false, settings.Value));
            else
                command.Parameters.Add(Helper.CreateIntParameter("@SettingValue", ParameterDirection.Input, false, DBNull.Value));            
            
            command.Parameters.Add(Helper.CreateIntParameter("@SettingId", ParameterDirection.Input, false, settings.Id));

            command.ExecuteNonQuery();
        }

        private static ESetting BuildSetting(SqlCeDataReader dr, SettingsType type)
        {
            ESetting setting = new ESetting();

            setting.Id = dr.GetInt32(dr.GetOrdinal("SettingId"));

            if (!dr.IsDBNull(dr.GetOrdinal("SettingName")))
                setting.Name = dr.GetString(dr.GetOrdinal("SettingName"));

            if (!dr.IsDBNull(dr.GetOrdinal("SettingValue")))
                setting.Value = dr.GetInt32(dr.GetOrdinal("SettingValue"));

            setting.SettingsType = type;

            return setting;
        }
    }
}
